
import dir from '../../assets/images/dir.jpg';
import up from '../../assets/images/up.jpg';
import up_active from '../../assets/images/up_active.jpg';
import info from '../../assets/images/info.jpg';
import info_active from '../../assets/images/info_active.jpg';
import user from '../../assets/images/user.jpg';
import user_active from '../../assets/images/user_active.jpg';
import down from '../../assets/images/down.jpg';
import down_active from '../../assets/images/down_active.jpg';
import { getFileAndFolder } from '../../api/index.js'
import { dateFormat } from '../../utils/index.js'
Page({
  data() {
    return {
      up,
      up_active,
      info,
      info_active,
      user,
      user_active,
      down,
      down_active,
      loading: false,
      dir: dir,
      sortVal: 2,
      viewVal: 2,
      drawerShow: false,
      sortShow: false,
      viewShow: false,
      downloadMethodShow: false,
      customList: [],
      folderList: [],
      fileList: [],
      panType: '',
      downloadMoreShow:false,
      folderPathes: [{ name: "全部文件", id: '' }],
      fileAndFolderId:'',
      appInstance:null,
      pageNum:1,
      pageSize:300,
      sortField:"updated_time",
      sortOrder:'DESC',
      aliNextMarker:"",
      isFinished: false,
      direction: [ 'bottom'],
      indicator: {
        activate: '松开即可刷新',
        deactivate: '下拉可以刷新',
        release: '加载中...',
        finish: '刷新成功'
      },
      isFinished_view: false,
      direction_view: [ 'bottom'],
      indicator_view: {
        activate: '松开即可刷新',
        deactivate: '下拉可以刷新',
        release: '加载中...',
        finish: '刷新成功'
      },
      panSelect:0,
      tiYiSelectedList:[],
      isDownloaded:false
    }
  },
  watch:{
    downloadMethodShow:function(n,o){
   
      if(!n){

        this.customList.forEach(val=>{
         
          // this.$set(val,'checkValue',false)
        })
    
      }
    },
    viewVal:function(n,o){
    
      this.appInstance.setData({viewVal: n})
    },
    sortVal:function(n,o){

      this.appInstance.setData({sortVal: n})
    }
  },
  methods: {
    downloadClick(){
      if(this.tiYiSelectedList.length!==0){
        this.downloadMethodShow=true
      }
     
    },
    refresh () {
      console.log("这里是下拉")
      // setTimeout(()=>{
      //   this.isFinished = true
      // },2000)
      this.pageNum++
      this.getFileAndFolder(true)
    },
    refresh_view () {
      console.log("这里是下拉2")
      // setTimeout(()=>{
      //   this.isFinished = true
      // },2000)
      this.pageNum++
      this.getFileAndFolder(true)
    },
    changeBack(val) {
      console.log("选中的安妮===>", val);
    },
    changeSort(val) {
      this.sortShow = true
    },
    editSort(num) {
      this.sortShow = false
      this.sortVal = num

      if(this.sortVal===0){
        console.log('这里是0');
        this.sortField="name";
        this.sortOrder='ASC'
        this.pageNum = 1
        this.aliNextMarker = ''
      }
      if(this.sortVal===1){
        this.sortField="size";
        this.sortOrder='DESC'
        this.pageNum = 1
        this.aliNextMarker = ''
      }
      if(this.sortVal===2){
        this.sortField="updated_time";
        this.sortOrder='DESC'
        this.pageNum = 1
        this.aliNextMarker = ''
      }
      this.getFileAndFolder()
      // this.sortList(num)
    },

    // sortList(sortVal) {
    //   // 现在的排序为后台逻辑,  所以前端的排序暂时注释
    //   this.customList = [...this.folderList, ...this.fileList]
    // },
    changeView(val) {
      this.viewShow = true
    },
    editChangeView(num) {
      this.viewVal = num
      this.viewShow = false
    },

    checkChangeValue(val, file) {
      // if (val) {
      //   ok.setH5AppStorage({
      //     key: 'downFiles',
      //     data: [file]
      //   }).catch(err => {
      //     console.log("接口调用异常", err);
      //   });
      //   this.downloadMethodShow = true
      // }
      let item = this.customList.find((item) => {
        return item.id === file.id
      })
      item.checkValue = val
      this.tiYiSelectedList = this.customList.filter((item) => {
        return item.checkValue
      })

      ok.setH5AppStorage({
        key: 'downFiles',
        data: [...this.tiYiSelectedList]
      }).catch(err => {
        console.log("接口调用异常", err);
      });
    },
    clickDownload() {
      this.downloadMethodShow = true
    },
    goToEntrance(){
      this.$router.push({ path: '/pages/entrance/index' })
    },
    downloadMethodHandle() {
      this.downloadMethodShow = false
      this.isDownloaded = true
      this.$router.replace({ path: '/pages/yunpanlist/index' })
    },
    downloadMoreHandle() {
      this.downloadMoreShow = false
      this.$router.replace({ path: '/pages/more/index'})
    },

    async getFileAndFolder(isFinished) {
      let panType = this.panType
      let pageNum = this.pageNum
      let pageSize = this.pageSize
      let sortField = this.sortField
      let sortOrder = this.sortOrder
      let aliNextMarker = this.aliNextMarker
      this.loading = true
      const { data } = await getFileAndFolder({
        pageNum,
        pageSize,
        sortField,
        sortOrder,
        panType,
        panSelect:this.panSelect,
        aliNextMarker,
        fileAndFolderId: this.fileAndFolderId=='/全部文件' ? '' :this.fileAndFolderId
      })

      this.loading = false
      if (data.code === 500&&data.msg==='用户登录已超时，请重新登录') {
       
        ok.clearH5AppStorage().then(res=>{
          this.$router.replace('/pages/login/index')
        })
        .catch(err=> {
          console.log("接口调用异常",err);
        });
        return
      }
      let folderList = data.data.folderList.map(val => {
        val.checkValue = false
        val.time = val.updateTime
        return val
      })
      let fileList = []

      if(Array.isArray(data.data.fileList)){
         fileList = data.data.fileList.map(val => {
  
          val.checkValue = false
          val.time = val.updateTime
          return val
        })
      }else{
        fileList = []
      }
      
      // this.folderList = folderList
      // this.fileList = fileList
      // this.customList = [...this.folderList, ...this.fileList]
      // this.sortList(this.sortVal)

      this.aliNextMarker = data.data.aliNextMarker
      console.log(this.aliNextMarker)



      if(isFinished){
        if(folderList.length>0){
          this.folderList.push(...folderList)
        } 
        if(fileList.length>0){
          this.fileList.push(...fileList)
        }
       
        this.customList = [...this.folderList, ...this.fileList]
        console.log("进来了");
        console.log(this.folderList.length);
        console.log(this.fileList.length);
        console.log(this.customList.length);
        this.isFinished = true
        this.isFinished_view = true
      }else{
        this.folderList = folderList
        this.fileList = fileList
        this.customList = [...this.folderList, ...this.fileList]
      }

      // this.loading = false

    },


    gotoMore(){
      this.downloadMoreShow = true
    },

    backFilePath(){
      console.log("点击了后面",this.folderPathes.length);
      if(this.panSelect!=0&&this.folderPathes.length==1){
        
        this.panSelect = 0
        ok.setH5AppStorage({
          key: 'panSelect',
          data: this.panSelect
        })
        this.editSort(this.sortVal)
        return
      }
      if (this.folderPathes.length - 2 < 0) return
      this.tiYiPathClick(this.folderPathes[this.folderPathes.length - 2])
    },
    tiYiPathClick(val) {
      let pathes = this.folderPathes
      let cur = pathes[pathes.length - 1]
      if (cur.id == val.id) {
        return
      }
      this.fileAndFolderId = val.id
      if(this.panType=='2'){
        this.fileAndFolderId = val.parentId ? val.parentId + '/' + val.name : '/' + val.name
      }
      while (true) {
        let cur = pathes.pop()
        // 说明这是最后一个
        if (cur.id == val.id) {
          pathes.push(cur)
          break
        }
      }
      this.pageNum=1
      this.aliNextMarker=''
      this.tiYiSelectedList = []
      this.getFileAndFolder()
    },

    tiYiFolderClick(item) {
      if (!item.fileType) {
        this.fileAndFolderId = item.id
        if(this.panType=='2'){
          this.fileAndFolderId = item.parentId ? item.parentId + '/' + item.name : '/' + item.name
        }
        this.folderPathes.push({
          id: item.id,
          name: item.name,
          parentId:item.parentId
        })
        this.pageNum=1
        this.aliNextMarker=''
        this.tiYiSelectedList = []
        this.getFileAndFolder()
      }
    },

    aliPanClick(item){
      this.panSelect = item.id

      ok.setH5AppStorage({
        key: 'panSelect',
        data: this.panSelect
      })
      this.editSort(this.sortVal)
    }


  },

  computed:{
    navTitle:function(){
      return this.panType === '2'?'百度网盘':'阿里云盘'
    }
  },
  created(){
    this.appInstance = getApp()
    this.sortVal=this.appInstance.data().sortVal !== undefined ? this.appInstance.data().sortVal : 2
    this.viewVal=this.appInstance.data().viewVal !== undefined ? this.appInstance.data().viewVal : 2
  },
  mounted() {
    ok.setH5AppStorage({
      key: 'panSelect',
      data: 0
    })
   
    if (this.$route.query.panType) {
      this.panType = this.$route.query.panType
      ok.setH5AppStorage({
        key: 'panType',
        data: this.$route.query.panType
      }).catch(err => {
        console.log("接口调用异常", err);
      });
      this.editSort(this.sortVal)
      // this.getFileAndFolder()
    } else {
      ok.getH5AppStorage({
        key: 'panType'
      }).then(res => {
        this.panType = res.data.data
        this.editSort(this.sortVal)
        // this.getFileAndFolder()
      }).catch(err => {
        console.log("接口调用异常", err);
      });
    }


  },
  destroyed(){
    // ok.showNavigationBar()
    // .catch(err=> {
    //   console.log("接口调用异常",err);
    // });
    if(!this.isDownloaded){
      ok.setH5AppStorage({
        key: 'downFiles',
        data: []
      }).catch(err => {
        console.log("接口调用异常", err);
      });
    }
  }
});
